package project.crawler.downloader;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;

public class PageDownloader {

	private static final String USER_URL_PREFIX = "http://stackoverflow.com/users/";
	private static final String QUESTION_URL_PREFIX = "http://stackoverflow.com/questions/";

	private static final long SLEEP_TIME_MILLS = 1000;

	public static String downloadUserRawHtml(long userID) throws IOException {
		return downloadUrl(USER_URL_PREFIX + userID);
	}

	public static String downloadQuestionRawHtml(long questionID)
			throws IOException {
		return downloadUrl(QUESTION_URL_PREFIX + questionID);
	}

	public static void main(String[] args) throws IOException {
		if (args.length != 1 && args.length != 2) {
			System.out.println("Usage : DownloadPage <URL>");
			System.out.println("   or : DownloadPage <URL> <File>");
			return;
		}
		String data = downloadUrl(args[0]);

		Writer out;
		if (args.length == 1) {
			out = new OutputStreamWriter(System.out);
		} else {
			out = new FileWriter(args[1]);
		}

		out.write(data);
		out.flush();
		out.close();
	}

	public static String downloadUrl(String url) throws IOException {
		System.out.println("Downloading url: " + url);
		URL u = new URL(url);
		InputStream in = u.openStream();
		StringBuilder sb = new StringBuilder();
		int a;
		while ((a = in.read()) >= 0) {
			sb.appendCodePoint(a);
		}
		in.close();
		
		System.out.println("Download success, sleeping for " + SLEEP_TIME_MILLS
				+ " milliseconds...");
		try {
			Thread.sleep(SLEEP_TIME_MILLS);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
		
		return sb.toString();
	}
}
